package com.hp.gxw.api;

import com.hp.gxw.constant.R;
import com.hp.gxw.entity.Product;
import com.hp.gxw.service.IProductService;
import com.hp.gxw.u8.EFWebService;
import com.hp.gxw.u8.U8ApiUtils;
import com.hp.gxw.u8.U8WebXMLResponseU8WebXMLResult;
import com.hp.gxw.u8.dto.InventoryParameter;
import com.hp.gxw.u8.dto.U8ApiResultDto;
import com.hp.gxw.u8.xml.InventoryXml;
import com.hp.gxw.utils.DateUtil;
import org.apache.axis.message.MessageElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

@Component
@RequestMapping("/api/inventory")
public class InventoryApi {

    @Autowired
    private U8ApiUtils u8ApiUtils;

    @Autowired
    private IProductService iProductService;

	@Autowired
	private EFWebService eFWebServiceLocator;

    @Value("${u8client.serviceId}")
    private String serviceId;

    @Value("${u8client.inventoryUnit}")
    private String inventoryUnit;

    @Value("${u8client.inventoryUnitGroup}")
    private String inventoryUnitGroup;

    @Value("${u8client.inventoryType}")
    private String inventoryType;

    @Value("${u8client.environment}")
    private String environment;

    @RequestMapping("/register")
    @ResponseBody
    public R inventory_register(@RequestBody Map<String, Object> map){
        if(map == null){
            return R.error("参数为空");
        }
        InventoryParameter inventoryParameter = new InventoryParameter();
        Product product = new Product();
        if(map.get("code") == null){
            return R.error("code为必填项");
        }
        inventoryParameter.setCode(map.get("code").toString());
        product.setCode(map.get("code").toString());
        if(map.get("name") == null){
            return R.error("name为必填项");
        }
        inventoryParameter.setName(map.get("name").toString());
        if(map.get("cInvAddCode") != null){
            inventoryParameter.setCInvAddCode(map.get("cInvAddCode").toString());
        }
        product.setName(map.get("name").toString());
//        if(map.get("sort_code") == null){
//            return R.error("sort_code为必填项");
//        }
//        inventoryParameter.setSort_code(map.get("sort_code").toString());
        inventoryParameter.setSort_code(inventoryType);
//        if(map.get("unitgroup_code") == null){
//            return R.error("unitgroup_code为必填项");
//        }
//        inventoryParameter.setUnitgroup_code(map.get("unitgroup_code").toString());
        inventoryParameter.setUnitgroup_code(inventoryUnitGroup);
//        if(map.get("main_measure") == null){
//            return R.error("main_measure为必填项");
//        }
//        inventoryParameter.setMain_measure(map.get("main_measure").toString());
        inventoryParameter.setMain_measure(inventoryUnit);
        if(map.get("sale_flag") != null){
            inventoryParameter.setSale_flag(map.get("sale_flag").toString());
        }else{
            inventoryParameter.setSale_flag("1");
        }
        if(map.get("purchase_flag") != null){
            inventoryParameter.setPurchase_flag(map.get("purchase_flag").toString());
        }
        if(map.get("selfmake_flag") != null){
            inventoryParameter.setSelfmake_flag(map.get("selfmake_flag").toString());
        }
        if(map.get("prod_consu_flag") != null){
            inventoryParameter.setProd_consu_flag(map.get("prod_consu_flag").toString());
        }
        if(map.get("in_making_flag") != null){
            inventoryParameter.setIn_making_flag(map.get("in_making_flag").toString());
        }
        if(map.get("tax_serv_flag") != null){
            inventoryParameter.setTax_serv_flag(map.get("tax_serv_flag").toString());
        }else{
            inventoryParameter.setTax_serv_flag("1");
        }
        if(map.get("CreatePerson") != null){
            inventoryParameter.setCreatePerson(map.get("CreatePerson").toString());
            inventoryParameter.setModifyPerson(map.get("CreatePerson").toString());
        }else{
            inventoryParameter.setCreatePerson("api");
            inventoryParameter.setModifyPerson("api");
        }
        if(map.get("ModifyDate") != null){
            inventoryParameter.setModifyDate(DateUtil.getDateTimeFormat(map.get("ModifyDate").toString()));
        }else{
            inventoryParameter.setModifyDate(new Date());
        }
        if(map.get("pricetype") != null){
            inventoryParameter.setPricetype(map.get("pricetype").toString());
        }else{
            inventoryParameter.setPricetype("全月平均法");
        }
        if(map.get("cPlanMethod") != null){
            inventoryParameter.setCPlanMethod(map.get("cPlanMethod").toString());
        }else{
            inventoryParameter.setCPlanMethod("R");
        }
        if(map.get("cSRPolicy") != null){
            inventoryParameter.setCSRPolicy(map.get("cSRPolicy").toString());
        }else{
            inventoryParameter.setCSRPolicy("PE");
        }
        if(map.get("iSupplyType") != null){
            inventoryParameter.setISupplyType(map.get("iSupplyType").toString());
        }else{
            inventoryParameter.setISupplyType("0");
        }
        inventoryParameter.setEfserverid(serviceId);
        if(environment.equals("pro")){
            U8WebXMLResponseU8WebXMLResult u8WebXML = null;
            try {
                u8WebXML = eFWebServiceLocator.getEFWebServiceSoap().u8WebXML(InventoryXml.createXML(inventoryParameter));
                //解析返回结果
                MessageElement[] data = u8WebXML.get_any();
                System.out.println(data[0].getAttribute("succeed").toString());
                System.out.println(data[0].getAttribute("dsc").toString());
                if("0".equals(data[0].getAttribute("succeed").toString())) {
                    return R.error("接口异常"+data[0].getAttribute("dsc").toString());
                }
                product.setCreateTime(new Date());
                iProductService.save(product);
            }  catch (Exception e) {
                e.printStackTrace();
                return R.error("接口异常");
            }
        }else if(environment.equals("dev")){
            try {
                U8ApiResultDto resultDto = u8ApiUtils.httpRequest(InventoryXml.createXML(inventoryParameter).split("\n")[1]);
                if(resultDto == null || "0".equals(resultDto.getCode())) {
                    return R.error(resultDto==null?"接口异常":resultDto.getMsg());
                }
                product.setCreateTime(new Date());
                iProductService.save(product);
            }catch (Exception e){
                e.printStackTrace();
                return R.error("接口异常");
            }
        }
        return R.ok("写入成功");
    }
}
